<template>
  <a-modal
    title="操作"
    :width="800"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="服务商"
        >
          <a-select :disabled="isDisabled"
                    v-decorator="[ 'businessServiceId', {rules: [{ required: true, message: '请选择。。。' }]} ]">
            <a-select-option v-for="item in businessList" :key="item.id">{{item.name}}</a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="设备生产厂家"
        >
          <a-input placeholder="设备生产厂家"
                   v-decorator="[ 'createdFactory', {rules: [{ required: true, message: '请输入。。。' }]} ]"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="设备型号"
        >
          <a-input placeholder="设备型号" v-decorator="[ 'modelNo', {rules: [{ required: true, message: '请输入。。。' }] }]"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="设备编号"
        >
          <a-input placeholder="设备编号" @blur="validateDeviceNo"
                   v-decorator="[ 'deviceNo', {rules: [{ required: true, message: '请输入。。。' }] }]"/>
        </a-form-item>

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="设备状态">
          <a-radio-group v-decorator="[ 'status', {rules: [{ required: true, message: '请选择' }]}]">
            <a-radio :value="0">禁用</a-radio>
            <a-radio :value="1">启用</a-radio>
          </a-radio-group>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { selectAll } from '@/api/businessService/business'
  import { save, selectByDeviceNo } from '@/api/deviceService/device'
  import pick from 'lodash.pick'

  export default {
    //--############  data() ###############-->
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        visible: false,
        confirmLoading: false,
        mdl: {},
        form: this.$form.createForm(this),
        businessList: [],
        validate: false,
        isDisabled: false
      }
    },
    //--############  data() ###############-->

    created() {
      selectAll().then(res => {
        this.businessList = res.result
      })
    },

    //--############  methods() ###############-->
    methods: {
      add() {
        this.isDisabled = false
        this.visible = true
      },
      edit(record) {
        this.isDisabled = true

        this.mdl = Object.assign({}, record)
        if (record.status) {
          this.mdl.status = 1
        } else {
          this.mdl.status = 0
        }
        this.mdl.businessServiceId = Number(record.businessServiceId)
        this.visible = true
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.mdl, 'businessServiceId','createdFactory', 'modelNo', 'deviceNo', 'status'))
        })
      },
      close() {
        this.$emit('close')
        this.visible = false
        this.form.resetFields()
        this.mdl = {}
      },
      handleOk() {
        const _this = this
        // 触发表单验证
        this.form.validateFields((err, values) => {
          // 验证表单没错误
          if (!err) {
            values.id = this.mdl.id
            _this.confirmLoading = true

            if(_this.validate){
              _this.$message.error('设备编号重复')
              _this.confirmLoading = false
              return
            }

            save(values).then((res) => {
              if (res.code === 200) {
                _this.$message.success('保存成功')
                _this.$emit('ok')
                _this.close()
              } else {
                this.$notification.error({
                  message: '错误',
                  description: res.message,
                  duration: 4
                })
              }
            }).catch(() => {
              // Do something
            }).finally(() => {
              _this.confirmLoading = false
            })
          }
        })
      },
      handleCancel() {
        this.close()
      },
      validateDeviceNo(e) {
        const _this = this
        selectByDeviceNo(e.target.value).then(res => {
          if (res.code != 200) {
            _this.$message.error(res.message)
            _this.validate = true
          } else {
            _this.validate = false
          }
        })
      }
    }
    //--############  methods() ###############-->
  }
</script>

<style scoped>

</style>
